#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QString>
#include<QElapsedTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tmp="";
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setNum1(double num)  //函数：获取第一个
{
    num1=num;
}
void MainWindow::setNum2(double num)//函数，获取第二个
{
    num2=num;
}
void MainWindow::setFlag(QString flag)//函数，获取符号
{
    this->flag=flag;
}

double MainWindow::doExpr()  //函数，运算
{
    double result = 0;
    if(flag=="+")
        result=num1+num2;
    else if (flag=="-") {
        result=num1-num2;
    }
    else if (flag=="*") {
        result=num1*num2;
    }
    else if (flag=="/") {
        if(num2 == 0)
            return 666666666;
        result=num1/num2;
    }
    return result;
}





void MainWindow::on_pushBTdot_clicked()
{
    tmp +=this->ui->pushBTdot->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT0_clicked()
{
    tmp +=this->ui->pushBT0->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT1_clicked()
{
    tmp +=this->ui->pushBT1->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT2_clicked()
{
    tmp +=this->ui->pushBT2->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT3_clicked()
{
    tmp +=this->ui->pushBT3->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT4_clicked()
{
    tmp +=this->ui->pushBT4->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT5_clicked()
{
    tmp +=this->ui->pushBT5->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT6_clicked()
{
    tmp +=this->ui->pushBT6->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT7_clicked()
{
    tmp +=this->ui->pushBT7->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT8_clicked()
{
    tmp +=this->ui->pushBT8->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBT9_clicked()
{
    tmp +=this->ui->pushBT9->text();
    this->ui->label->setText(tmp);
}

void MainWindow::on_pushBTaad_clicked() //加按钮
{
    //按加法之前，需要int一下用来保存第一个数据
    double num =tmp.toDouble();
    MainWindow::setNum1(num);
    //进而马上再将tmp清空
    tmp ="";
    QString ex=ui->pushBTaad->text();
    MainWindow::setFlag(ex);
    this->ui->label->setText(ex);
}

void MainWindow::on_pushBTsub_clicked() //减按钮
{
    //按减法之前，需要int一下用来保存第一个数据
    double num =tmp.toDouble();
    MainWindow::setNum1(num);
    //进而马上再将tmp清空
    tmp ="";
    QString ex = ui->pushBTsub->text();
    MainWindow::setFlag(ex);
    this->ui->label->setText(ex);
}

void MainWindow::on_pushBTmul_clicked() //乘按钮
{
    //按乘法之前，需要int一下用来保存第一个数据
    double num =tmp.toDouble();
    MainWindow::setNum1(num);
    //进而马上再将tmp清空
    tmp ="";
    QString ex=ui->pushBTmul->text();
    MainWindow::setFlag(ex);
    this->ui->label->setText(ex);
}

void MainWindow::on_pushBTdiv_clicked()//除按钮
{
    //按除法之前，需要int一下用来保存第一个数据
    double num =tmp.toDouble();
    MainWindow::setNum1(num);
    //进而马上再将tmp清空
    tmp ="";
    QString ex=ui->pushBTdiv->text();
    MainWindow::setFlag(ex);
    this->ui->label->setText(ex);
}

void MainWindow::on_pushBTequ_clicked()   //“等于”按钮
{
    //将运算符之后的tmp转整型后 进入num2函数
    double num = tmp.toDouble();
    MainWindow::setNum2(num);
    //此时doExpr函数已经得到对应的参数了
    double result =doExpr(); //计算函数
    QString result1=QString::number(result);  //将计算结果转为字符，至此计算完毕

    //将前者数据显示，输出更直观
    QString Num1Str=QString::number(num1);
    QString Num2Str=QString::number(num2);
    Num1Str.append(flag).append(Num2Str).append("=").append(result1); //将前者数据显示，输出更直观
    ui->label->setText(Num1Str);
    tmp ="";
}

void MainWindow::on_pushBTcle_clicked() //清零按钮
{
    setNum1(0);  //将一号数字和二号数字清零，计算符不需要设置，下次计算会自动覆盖
    setNum2(0);
    tmp="";//这里必须清，不然在2+3清零的时候，下一次会出现3
    this->ui->label->setText("0");
}
